package in.antany.eclipse.file.handler.helper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BuildPathUtils {

	public static void copyFilesIntoBuildPath(String srcFileName,
			String srcFolderName) {
		HashMap<String, BuildCopyRule> ruleMap = new HashMap<>();
		genRulesMaps(ruleMap);
		BuildCopyRule copyRule = ruleMap.get(srcFolderName);
		if (copyRule.isJavaFile()) {
			srcFileName = srcFileName.replaceAll(".java", ".class");
			srcFolderName = copyRule.getClassFolder();
		}

		String srcFilePath = srcFolderName + "\\" + srcFileName;
		String tgtFilePath = copyRule.getTgtFolder() + "\\" + srcFileName;

		if (!copyRule.isJarFile()) {
			try (FileChannel source = new FileInputStream(srcFilePath)
					.getChannel();
					FileChannel target = new FileOutputStream(tgtFilePath)
							.getChannel();) {
				target.transferFrom(source, 0, source.size());
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			Path sourceFile = Paths.get(srcFilePath);
			Path targetFile = Paths.get(copyRule.getJarFile());
			FileSystem fs = null;
			try {
				fs = FileSystems.newFileSystem(targetFile, null);
				Path tgtPath = fs.getPath(copyRule.getTgtFolder());
				if (!Files.exists(tgtPath)) {
					Files.createDirectories(tgtPath);
				}

				Path fileInsideZipPath = fs.getPath(copyRule.getTgtFolder() + ""
						+ srcFileName);

				Files.copy(sourceFile, fileInsideZipPath,
						StandardCopyOption.REPLACE_EXISTING);
				fs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	private static void genRulesMaps(HashMap<String, BuildCopyRule> ruleMap) {
		String ruleFileName = "anteclipse.dsl";
		String userHome = System.getProperty("user.home");
		String ruleFilePath = userHome + "\\" + ruleFileName;
		File file = new File(ruleFilePath);
		char[] chrbuff = new char[(int) file.length()];
		try (BufferedReader buffReader = new BufferedReader(
				new FileReader(file))) {
			buffReader.read(chrbuff);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		String str = new String(chrbuff);
		Pattern patternRule = Pattern.compile("(?s)\\{.*?\\}");
		Matcher matcherRule = patternRule.matcher(str);
		while (matcherRule.find()) {
			BuildCopyRule buildCopyRule = getBuildCopyRuleFromDSLString(matcherRule
					.group(0));
			ruleMap.put(buildCopyRule.getSrcFolder(), buildCopyRule);
		}
	}

	public static BuildCopyRule getBuildCopyRuleFromDSLString(String str) {
		java.lang.reflect.Method method;
		BuildCopyRule buildCopyRule = new BuildCopyRule();
		Pattern pattenParams = Pattern.compile("(.*?)=(.*?);");
		Matcher matcherParams = pattenParams.matcher(str);
		while (matcherParams.find()) {
			String paramName = matcherParams.group(1).trim();
			String valueName = matcherParams.group(2).trim();
			String methodName = "set" + paramName.substring(0, 1).toUpperCase()
					+ paramName.substring(1);
			try {
				method = buildCopyRule.getClass().getMethod(methodName,
						String.class);
				method.invoke(buildCopyRule, valueName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return buildCopyRule;
	}

	public static void main(String[] args) {
		String folderName = "C:\\s1\\java\\";
		String fileName = "Main.java";
		copyFilesIntoBuildPath(fileName, folderName);
	}
}
